Show the code
library(maps)
library(viridis)
library(tidyverse)
library(leaflet)
library(sf)
library(tigris)
# Load the USArrests dataset
data(USArrests)
# Prepare the data for leaflet
arrests_data <- USArrests %>%
rownames_to_column(var = "state") %>%
mutate(state = tolower(state))
# Load the US states map data
us_states_map <- map_data("state")
# Define color palette
color_pal <- colorBin(viridis_pal()(5), domain = arrests_data$Murder, bins = 5)
# Join the US states map data with the arrests data
us_states_map_data <- us_states_map %>%
left_join(arrests_data, by = c("region" = "state"))
# Load the US states shapefile data
us_states_shapefile <- states(class = "sf", resolution = "20m") %>%
st_transform(4326) %>%
mutate(state = tolower(NAME))
|
| | 0%
|
| | 1%
|
|= | 1%
|
|= | 2%
|
|== | 2%
|
|== | 3%
|
|=== | 4%
|
|=== | 5%
|
|==== | 5%
|
|==== | 6%
|
|===== | 7%
|
|===== | 8%
|
|====== | 8%
|
|====== | 9%
|
|======= | 9%
|
|======= | 10%
|
|======== | 11%
|
|======== | 12%
|
|========= | 12%
|
|========= | 13%
|
|========== | 14%
|
|=========== | 15%
|
|=========== | 16%
|
|============ | 17%
|
|============= | 18%
|
|============= | 19%
|
|============== | 20%
|
|============== | 21%
|
|================ | 23%
|
|================= | 25%
|
|================== | 25%
|
|================== | 26%
|
|==================== | 28%
|
|===================== | 31%
|
|====================== | 31%
|
|====================== | 32%
|
|======================== | 34%
|
|======================== | 35%
|
|========================= | 36%
|
|============================ | 40%
|
|=============================== | 44%
|
|================================== | 48%
|
|===================================== | 52%
|
|======================================== | 57%
|
|=========================================== | 61%
|
|============================================= | 65%
|
|================================================ | 69%
|
|=================================================== | 72%
|
|=================================================== | 73%
|
|===================================================== | 76%
|
|====================================================== | 77%
|
|======================================================= | 78%
|
|======================================================= | 79%
|
|======================================================== | 80%
|
|======================================================== | 81%
|
|========================================================= | 81%
|
|========================================================= | 82%
|
|============================================================ | 85%
|
|============================================================= | 87%
|
|============================================================== | 88%
|
|================================================================ | 91%
|
|================================================================ | 92%
|
|================================================================= | 93%
|
|================================================================== | 94%
|
|================================================================== | 95%
|
|=================================================================== | 96%
|
|==================================================================== | 97%
|
|===================================================================== | 98%
|
|===================================================================== | 99%
|
|======================================================================| 99%
|
|======================================================================| 100%
Show the code
# Join the US states shapefile data with the arrests data
us_states_map_data <- us_states_shapefile %>%
left_join(arrests_data, by = c("state" = "state"))